<?php
defined('IN_TS') or die('Access Denied.');
//程序主体
switch($ts){
	case "":
		if($TS_USER['user'] != '') qiMsg("已经登陆啦!");

		$title = '登录';
		include TmooTemplate("login");
		break;
	
	//执行登录
	case "do":
		if (IS_POST) {
			if($TS_USER['user'] != '') header("Location: index.php");
			
			$email = trim($_POST['email']);
			$pwd = md5(trim($_POST['pwd']));
			
			$cktime = $_POST['cktime'];
		
			$userNum	= $DB->once_num_rows("select * from ".dbprefix."app_user where email='$email' and pwd='$pwd'");
			
			$emailNum = $DB->once_num_rows("select * from ".dbprefix."app_user where email='$email'");
			
			if($email=='' || $pwd==''){
				qiMsg("所有输入项都不能为空^_^");
			}elseif(valid_email($email) == false){
				qiMsg("Email书写不正确^_^");
			}elseif($emailNum == '0'){

				qiMsg("你还没有注册呢，请注册吧^_^");
				
			}elseif($emailNum > '0' && $userNum == '0'){
				
				qiMsg("密码输入有误，忘记可以找回密码^_^");
				
			}else{
			
				$userData	= $DB->once_fetch_assoc("select  * from ".dbprefix."app_user_info where email='$email'");
				
				//记住登录Cookie
				 if($cktime != ''){   
					 setcookie("ts_email", $email, time()+$cktime);   
					 setcookie("ts_pwd", $pwd, time()+$cktime);
				 }   
				
				//用户session信息
				$_SESSION['tsuser']	= $userData;
				
				//用户userid
				$userid = $userData['userid'];
				
				
				//积分记录
				$DB->query("insert into ".dbprefix."app_user_scores (`userid`,`scorename`,`score`,`addtime`) values ('".$userid."','登录','10','".time()."')");
				
				$strScore = $DB->once_fetch_assoc("select sum(score) score from ".dbprefix."app_user_scores where userid='".$userid."'");
				
				//更新登录时间
				$DB->query("update ".dbprefix."app_user_info set `uptime`='".time()."' , `count_score`='".$strScore['score']."' where userid='$userid'");

				header('Location: index.php');
			
			}
		}
		
		break;
	
	//退出	
	case "out":
		session_destroy();
		setcookie("ts_email", '', time()+3600);   
		setcookie("ts_pwd", '', time()+3600);
		header('Location: index.php');
		break;
}